/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.constraint;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.regex.Pattern;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author avisto
 */
@Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = NumSecuConstraint.NumSecuValidator.class)
public @interface NumSecuConstraint {

    static final Logger logger = LoggerFactory.getLogger(NumSecuConstraint.class);

    String message() default "{fr.adama.constraint.numsecu.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    public class NumSecuValidator implements ConstraintValidator<NumSecuConstraint, String> {

        Pattern p = Pattern.compile("^([1-3])[\\s\\.\\-]?([0-9]{2})[\\s\\.\\-]?(0[0-9]|[2-35-9][0-9]|[14][0-2])[\\s\\.\\-]?(0[1-9]|[1-8][0-9]|9[0-57-9]|2[ab])[\\s\\.\\-]?(00[1-9]|0[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|990)[\\s\\.\\-]?([0-9]{3})[\\s\\.\\-]?([0-8][0-9]|9[0-7])");

        @Override
        public void initialize(final NumSecuConstraint hasId) {
        }

        @Override
        public boolean isValid(final String numSecu, final ConstraintValidatorContext constraintValidatorContext) {
           
            return p.matcher(numSecu).find();
        }

    }

}
